package com.hong610.quartz.core;

import com.hong610.quartz.Quartz;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.triggers.CronTriggerImpl;

import java.text.ParseException;

/**
 * Created by Hong on 2017/2/8.
 */
public abstract class QuartzCore extends Quartz {

    /**
     * 获取TriggerKey
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @return TriggerKey
     */
    public TriggerKey getTriggerKey(String triggerName, String triggerGroup) {
        return new TriggerKey(triggerName, triggerGroup);
    }

    /**
     * 获取JobDetail
     *
     * @param jobClass    工作任务 包名.类型名
     * @param jobName     任务名
     * @param Description 描述
     * @return
     */
    private JobDetailImpl getJobDetail(String jobClass, String jobName, String Description) {
        JobDetailImpl job = new JobDetailImpl();
        try {
            job.setJobClass((Class<? extends Job>) Class.forName(jobClass));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        job.setName(jobName);
        job.setDescription(Description);
        return job;
    }

    /**
     * 获取触发器CronTrigger
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @return CronTriggerImpl
     */
    public CronTriggerImpl getCronTrigger(String triggerName, String triggerGroup) {
        try {
            return (CronTriggerImpl) getScheduler().getTrigger(new TriggerKey(triggerName, triggerGroup));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取触发器CronTrigger
     *
     * @param tk TriggerKey
     * @return CronTriggerImpl
     */
    public CronTriggerImpl getCronTrigger(TriggerKey tk) {
        try {
            return (CronTriggerImpl) getScheduler().getTrigger(tk);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加调度任务
     *
     * @param jobClass     工作任务 包名.类型名
     * @param jobName      任务名
     * @param description  描述
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @param cron         cron表达式
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    public void add(String jobClass, String jobName, String description, String triggerName, String triggerGroup, String cron) throws SchedulerException, ClassNotFoundException {
        Scheduler scheduler = getScheduler();
        JobDetailImpl job = getJobDetail(jobClass, jobName, description);
        CronTriggerImpl trigger = (CronTriggerImpl) TriggerBuilder.newTrigger()
                .withIdentity(triggerName, triggerGroup)
                .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build(); // 设置触发器 每20秒执行一次
        trigger.setDescription(description);
        trigger.setJobName(jobName);

        scheduler.scheduleJob(job, trigger);
        scheduler.start();
    }

    /**
     * 编辑调度任务
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @param newCron      新的cron表达式
     * @throws SchedulerException
     * @throws ParseException
     */
    public void edit(String triggerName, String triggerGroup, String newCron) throws SchedulerException, ParseException {
        Scheduler scheduler = getScheduler();
        TriggerKey tk = getTriggerKey(triggerName, triggerGroup);
        CronTriggerImpl trigger = getCronTrigger(tk);
        if (!trigger.getCronExpression().equals(newCron)) {
            trigger.setCronExpression(newCron);
            scheduler.rescheduleJob(tk, trigger);
        }
    }

    /**
     * 删除调度任务
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @throws SchedulerException
     */
    public void remove(String triggerName, String triggerGroup) throws SchedulerException {
        Scheduler scheduler = getScheduler();
        TriggerKey tk = getTriggerKey(triggerName, triggerGroup);
        scheduler.pauseTrigger(tk);
        scheduler.unscheduleJob(tk);
        scheduler.deleteJob(JobKey.jobKey(triggerName, triggerGroup));
    }

    /**
     * 暂停调度任务
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @throws SchedulerException
     */
    public void pause(String triggerName, String triggerGroup) throws SchedulerException {
        Scheduler scheduler = getScheduler();
        TriggerKey tk = getTriggerKey(triggerName, triggerGroup);
        scheduler.pauseTrigger(tk);
    }

    /**
     * 重启调度任务
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @throws SchedulerException
     */
    public void restart(String triggerName, String triggerGroup) throws SchedulerException {
        Scheduler scheduler = getScheduler();
        TriggerKey tk = getTriggerKey(triggerName, triggerGroup);
        CronTriggerImpl trigger = getCronTrigger(tk);
        scheduler.rescheduleJob(tk, trigger);
    }
}
